package com.xl.rpc.client;
import com.xl.rpc.codec.Decoder;
import com.xl.rpc.codec.Encoder;
import com.xl.rpc.common.utils.ReflectUtils;

import java.lang.reflect.Proxy;


/**
 * @author 辜圆圆
 * @description: gzy的项目
 * @date 2020/3/10 10:49 PM
 */
public class RpcClient {
    private RpcClientConfig config;
    private Encoder encoder;
    private Decoder decoder;
    private TransportSelector selector;

    public RpcClient(){
        this(new RpcClientConfig());
    }

    public RpcClient(RpcClientConfig config){
        this.config = config;
        this.encoder = ReflectUtils.newInstance(this.config.getEncoderClass());
        this.decoder = ReflectUtils.newInstance(this.config.getDevoderClass());
        this.selector = ReflectUtils.newInstance(this.config.getSelectorClass());
        this.selector.init(
                this.config.getServers(),
                this.config.getConnectCount(),
                this.config.getTransportClass()
        );


    }
    // 获取接口的代理对象
    public <T> T getProxy(Class<T> clazz){
        return (T)Proxy.newProxyInstance(
                getClass().getClassLoader(),
                new Class[]{clazz},
                new RemoteInvoker(clazz,encoder,decoder,selector)
                );

    }

}
